package com.glenet.salary.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.glenet.salary.entities.HrEmployeeExpand;
import com.glenet.salary.util.SessionFactoryUtil;

public class HrEmployeeExpandDao extends HrDaoSuport {
	
	public HrEmployeeExpand retrievalEmployeeExpand(String name, String idcard){
		HrEmployeeExpand expand = new HrEmployeeExpand();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			Map<String, String> map = new HashMap<String, String>();
			map.put("name", name);
			map.put("idcard", idcard);
			expand = (HrEmployeeExpand)session.selectOne("HrEmployeeExpand.retrieval", map);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return expand;
	}
	
	
	
	public boolean save(HrEmployeeExpand expand){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.insert("HrEmployeeExpand.save", expand) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public int batchSave(List<HrEmployeeExpand> list){
		int effect = 0;
		try {
			SqlSession session = SessionFactoryUtil.getSessionFactory().openSession(ExecutorType.BATCH);
			for (HrEmployeeExpand expand : list) {
				effect = +session.insert("HrEmployeeExpand.save", expand);
			}
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(effect);
		return effect;
	}
	
	public boolean update(HrEmployeeExpand expand){
		boolean flag = false;
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			flag = session.update("HrEmployeeExpand.update", expand) > 0 ? true : false;
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public HrEmployeeExpand get(Long id){
		HrEmployeeExpand expand = new HrEmployeeExpand();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			expand = (HrEmployeeExpand)session.selectOne("HrEmployeeExpand.get", id);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return expand;
	}
	
	@SuppressWarnings("unchecked")
	public List<HrEmployeeExpand> list(int offset, int limit){
		List<HrEmployeeExpand> list = new ArrayList<HrEmployeeExpand>();
		try {
			SqlSession session = SessionFactoryUtil.openSession();
			list = (List<HrEmployeeExpand>)session.selectList("HrEmployeeExpand.list", null, new RowBounds(offset, limit));
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
